package com.yld.demo.controller;


import com.alibaba.fastjson.JSON;
import com.yld.demo.entity.*;
import com.yld.demo.mapper.OrderListMapper;
import com.yld.demo.service.ProdInfoService;
import com.yld.demo.service.UserInfoService;
import com.yld.demo.util.kafka.KafkaData;
import com.yld.demo.service.OrderListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;
import yld.utils.ResultUtils;
import yld.utils.ResultVo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;


@RestController
@RequestMapping("/order")
public class OrderListController {
    @Autowired
    OrderListService orderListService;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    ProdInfoService prodInfoService;
    @Autowired
    KafkaTemplate<String, String> kafka;
    public static final String NUMBER_PATTERN = "^.*[0-9]+.*$";

    @GetMapping(path = "/selectAll")
    public ResultVo<OrderList> select() {
        return ResultUtils.success("查询成功", orderListService.selectAll());
    }

    @GetMapping(path = "/list")
    public ResultVo getOrderList(PageBean<OrderList> pageBean) throws InterruptedException, ParseException {
        return ResultUtils.success("分页展示", orderListService.getOrderList(pageBean));

    }

    @GetMapping(path = "/selectByUserId")
    public ResultVo<OrderList> selectByUserId(Integer userId, String startDate, String endDate) throws ParseException, InterruptedException {
        //判断输入格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setLenient(false);
        try {
            df.parse(startDate);
            df.parse(endDate);
            //判断用户是否存在
            UserInfo info = userInfoService.selectById(userId);
            if (info == null) {
                return ResultUtils.error("用户不存在");
            }

            List<OrderList> orderLists = orderListService.selectByUserId(userId, startDate, endDate);
            //查看有无订单，无，返回无订单
            if (orderLists.size() == 0) {
                return ResultUtils.error("该用户无订单");
            }
            //查询到订单并返回查询结果
            return ResultUtils.success("查询成功！", orderLists);

        } catch (ParseException e) {
            return ResultUtils.error("日期格式输入错误");
        }
    }

    @PostMapping(path = "/insert")
    public ResultVo<OrderList> insert(@RequestBody OrderListVo orderList) throws InterruptedException {
        //判断user_info内的user_id是否存在，如果存在则存入，否则报错！
        if (orderList != null) {
            //判断输入格式
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            df.setLenient(false);
            try {
                df.parse(orderList.getOrderDate());
            } catch (ParseException e) {
                return ResultUtils.error("日期格式输入错误");
            }
            UserInfo info = userInfoService.selectById(orderList.getUserId());
            ProdInfo prodInfo = prodInfoService.selectById(orderList.getProdId());

            if (info == null) {
                return ResultUtils.error("用户不存在");
            }
            if (prodInfo == null) {
                return ResultUtils.error("产品不存在");
            }
            if (orderList.getOrderNum() <= 0) {
                return ResultUtils.error("订单数量不符合规格");
            }
            if (prodInfo.getPrice() <= 0) {
                return ResultUtils.error("产品价格不符合规格");
            }
            if (prodInfo.getPrice() * orderList.getOrderNum() != orderList.getAmout()) {
                return ResultUtils.error("金额不匹配！");
            }
            KafkaData kafkaData = new KafkaData();
            kafkaData.setType("order_add");
            kafkaData.setData(JSON.toJSONString(orderList));
            kafka.send("test", JSON.toJSONString(kafkaData));
            return ResultUtils.success("新增发送成功");
        }
        return ResultUtils.error("输入为空！");
    }


    @DeleteMapping("/delete")
    private ResultVo<OrderList> delete(Integer orderId) {
        OrderList orderList = orderListService.getById(orderId);
        if (orderList == null || orderList.equals("")) {
            return ResultUtils.error("您要删除的订单不存在！！");
        } else {
            KafkaData kafkaData = new KafkaData();
            kafkaData.setType("order_delete");
            kafkaData.setData(orderId.toString());
            kafka.send("test", JSON.toJSONString(kafkaData));
            return ResultUtils.success("删除发送成功！");
        }
    }

    @PutMapping(path = "/update")
    public ResultVo UpdateOrderList(@RequestBody OrderListVo orderlist) throws InterruptedException {
        //判断输入格式

        if (Pattern.matches(NUMBER_PATTERN, String.valueOf(orderlist.getOrderNo())) == false) {
            return ResultUtils.error("用户id输入错误");
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setLenient(false);
        UserInfo info = userInfoService.selectById(orderlist.getUserId());
        ProdInfo prodInfo = prodInfoService.selectById(orderlist.getProdId());

        try {
            df.parse(orderlist.getOrderDate());
        } catch (ParseException e) {
            return ResultUtils.error("日期格式输入错误");
        }
        if (orderListService.selectById(orderlist.getOrderNo()) == null) {
            return ResultUtils.error("订单不存在");
        }
        if (info == null) {
            System.out.println("---------" + userInfoService.selectById(orderlist.getUserId()));
            return ResultUtils.error("用户不存在");
        } else if (prodInfo == null) {
            return ResultUtils.error("产品不存在");
        }
        if (orderlist.getOrderNum() <= 0) {
            return ResultUtils.error("订单数量不符合规格");
        }
        if (prodInfo.getPrice() <= 0) {
            return ResultUtils.error("产品价格不符合规格");
        }
        if (prodInfo.getPrice() * orderlist.getOrderNum() != orderlist.getAmout()) {
            return ResultUtils.error("金额不匹配！");
        } else {
            KafkaData kafkaData = new KafkaData();
            kafkaData.setType("order_update");
            kafkaData.setData(JSON.toJSONString(orderlist));
            kafka.send("test", JSON.toJSONString(kafkaData));
            return ResultUtils.success("更新发送成功");
        }
    }


}
